var _CONFIRM_DELETE = "\u786e\u5b9a\u8981\u5220\u9664";
var _DELETE_SUCCESS = "\u5220\u9664\u6210\u529f！";
var _CHECK_RECORD = "\u8bf7\u9009\u62e9\u8981\u64cd\u4f5c\u7684\u8bb0\u5f55！"


//=======start 弹出框==========
var isIe = (document.all) ? true : false;

function replaceJson(json) {
	json = json.replace(/%/g, "%25").replace(/&/g, "%26").replace(/=/g, "%3D");
	return json.replace(new RegExp('(["\\\\])', 'g'), '\\$1');
}
	
var replaceTd = function(jsonObj, td, rownum, toNull) {
	var h = $(td).html();
	var returnVal = h.replace("%7B", "{").replace("%7D", "}");//firefox
	if ($(td).attr("function")) {
		var fun = $(td).attr("function");
		var pattern = /\{\w+\}/g;		
		var token = pattern.exec(fun);
		var script = fun;
		while (token != null) {
			var val = eval("jsonObj." + token[0].replace("{", "").replace("}", ""));
			if (token[0] == "{_rownum}") val = rownum;			
			script = script.replace(new RegExp(token[0], 'g'), val);				
			token = pattern.exec(fun);				
		}
		returnVal = eval(script);
	}
	else {
		var pattern = /\{\w+\}/g;
		var token = pattern.exec(returnVal);
		while (token != null) {			
			var val = eval("jsonObj." + token[0].replace("{", "").replace("}", ""));
			if (token[0] == "{_rownum}") val = rownum;
			
			if (toNull) {
				if (val) returnVal = returnVal.replace(new RegExp(token[0], 'g'), val);
			}
			else {
				if (!val && val != 0) val = "";
				returnVal = returnVal.replace(new RegExp(token[0], 'g'), val);
			}			
			token = pattern.exec(h);			
		}		
	}
	return returnVal;
}

function getQueryJson(formObj) {
	if (!validateForm(formObj)) return -1;
	
	var fields = formObj.serializeArray();
	var a = [];
	for (var i = 0; i < fields.length; i++) {
		var f = fields[i];
		if ($.trim(f.value) != "") {
			a.push('"' + f.name + '":"' + $.trim(replaceJson(f.value)) + '"');		
		}
	}
	if (a.length == 0) {
		return '';
	}
	else {
		return '"' + formObj[0].name + '":{' + a + '}';
	}
}

function validateForm(formObj) {
	var fields = formObj.serializeArray();
	for (var i = 0; i < fields.length; i++) {
		var f = fields[i];
		
		// 验证textarea
		var textarea = $("textarea[name='" + f.name + "']");
		if (textarea.attr("maxlength") < textarea.text().length) {
			alert(textarea.attr("desc") + "\u957f\u5ea6\u4e0d\u80fd\u5927\u4e8e" + textarea.attr("maxlength") + "！");
			textarea[0].focus();
			return false;
		}	
		if (textarea.attr("validate") == "notNull" && $.trim(f.value) == "") {
			alert(textarea.attr("desc") + "\u4e0d\u80fd\u4e3a\u7a7a！");
			textarea[0].focus();
			return false;
		}
		
		// 验证select
		var select = $("select[name='" + f.name + "']");
		if (select.attr("validate") == "notNull" && $.trim(f.value) == "") {
			alert(select.attr("desc")+ "\u4e0d\u80fd\u4e3a\u7a7a！");
			select[0].focus();
			return false;
		}		
		
		// 验证input
		var input = $("input[name='" + f.name + "']");
		if (!input.attr("validate")) continue;
		var v = input.attr("validate").split(",");
		for (var j = 0; j < v.length; j++) {
			if (v[j] == "notNull" && $.trim(f.value) == "") {
				alert(input.attr("desc") + "\u4e0d\u80fd\u4e3a\u7a7a！");
				input[0].focus();
				return false;
			}
			if ($.trim(f.value) == "") break;
					
			if (v[j] == "number" && !/^\d+$/.test(f.value)) {
				alert(input.attr("desc") + "\u5fc5\u987b\u4e3a\u6574\u6570！");
				input[0].focus();
				return false;
			}
			else if (v[j] == "double" && !/^\d+((\.?\d+)|(\d*))$/.test(f.value)) {
				alert(input.attr("desc") + "\u5fc5\u987b\u4e3a\u6570\u5b57！");
				input[0].focus();
				return false;
			}
			else if (v[j] == "date" && !/^\d{4}\-\d{2}\-\d{2}$/.test(f.value)) {
				alert(input.attr("desc") + "\u5fc5\u987b\u4e3a\u683c\u5f0fyyyy-MM-dd,\u59822012-12-01！");
				input[0].focus();
				return false;
			}		
		}
	}
	return true;
}

function post(url, data, opTip, callback) {
	$.ajax({type: "POST", url:url, data:data, success:function(json) {
		if (opTip != null) {
			alert(opTip);
		}
		callback.call(this, json);
	}, error:function(XMLHttpRequest, textStatus, errorThrown) {
		document.write("json " + textStatus + XMLHttpRequest.responseText);
	}});
}

function showMask(p) {
	if (!p) p = "";
	
	// 如果没有设定_mask,刚默认为body
	// var w = document.body.clientWidth;
	// var h = document.body.clientHeight;
	
	var w = document.body.scrollWidth;
	var h = document.body.scrollHeight;
	var t = 0;
	var l = 0;
	var mask = $("#" + p + "_mask");
	if (mask[0]) {		
		w = mask.innerWidth();
		h = mask.innerHeight();
		t = mask.position().top;
		l = mask.position().left;
	}
	
	if (!$("#" + p + "_maskId")[0]) {
		$("body>*:eq(0)").after('<img id="' + p + "_loadingId" + '" src="' + basePath + 'b2b/img/loading.gif" style="position:absolute;">')
			.after('<div id="' + p + "_maskId" + '" style="background-color:gray;position:absolute;display:none;filter:Alpha(Opacity=30);-moz-opacity:0.3;opacity:0.3;"></div>');
	}
	
	$("#" + p + "_maskId")[0].style.top = t;
	$("#" + p + "_maskId")[0].style.left = l;
	$("#" + p + "_maskId").width(w);
	$("#" + p + "_maskId").height(h);
	$("#" + p + "_maskId").show();	
	$("#" + p + "_loadingId")[0].style.top = t + (h - $("#" + p + "_loadingId").height()) / 2;
	$("#" + p + "_loadingId")[0].style.left = l + (w - $("#" + p + "_loadingId").width()) / 2;	
	$("#" + p + "_loadingId").show();
}

function hideMask(p) {
	if (!p) p = "";		
	$("#" + p + "_maskId").hide();
	$("#" + p + "_loadingId").hide();
}

/**
 * 全选按钮的方法，这个方法使用在checkbox的点击方法
 * @param {Object} name  多选框的名
 * @param {Object} obj   被点击的多选框
 */
function allCheck(name,obj) {
	$("input[name='"+name+"']").attr("checked", obj.checked);
}

//弹出窗口 弹出位置在鼠标附近
function MessageBox(ev,title,content)
{
	var objPos = mousePosition(ev); 
	messContent="<div style='padding:20px 0 20px 0;text-align:center'>"+content+"</div>"; 
	showMessageBox(title,messContent,objPos,500); 
}

function mousePosition(ev) 
{ 
	if(ev)
	{
		if(ev.pageX || ev.pageY) 
		{ 
			return {x:ev.pageX, y:ev.pageY}; 
		} 
		return { 
			x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,y:ev.clientY + document.body.scrollTop - document.body.clientTop 
		}; 
	}
	else {
		return { 
			//x:document.body.clientWidth / 2 + document.body.scrollLeft+150,y:document.body.clientHeight / 2 + document.body.scrollTop
			x:document.documentElement.clientWidth / 2 + document.documentElement.scrollLeft+150,y:document.documentElement.clientHeight / 2 + document.documentElement.scrollTop
		};
	}
}

//弹出方法 
function showMessageBox(wTitle,content,pos,wWidth) 
{ 
	closeWindow(); 
	var bWidth=parseInt(document.documentElement.scrollWidth); 
	var bHeight=parseInt(document.documentElement.scrollHeight); 
	if(isIe){ 
		setSelectState('hidden');} 
	var back=document.createElement("div"); 
	back.id="back"; 
	var styleStr="top:0px;left:0px;position:absolute;background:#666;width:"+bWidth+"px;height:"+bHeight+"px;z-index:1000000;"; 
	styleStr+=(isIe)?"filter:alpha(opacity=0);":"opacity:0;"; 
	back.style.cssText=styleStr; 
	document.body.appendChild(back); 
	showBackground(back,50); 
	var mesW=document.createElement("div"); 
	mesW.id="mesWindow"; 
	//mesW.className="mesWindow"; 
	styleStr = "border:#666 1px solid;background:#fff;z-index:1000000;";
	//mesW.style.cssText=styleStr; 
	mesW.innerHTML="<div style='border-bottom:#eee 1px solid;margin-left:4px;padding:3px;font-weight:bold;text-align:left;font-size:12px;z-index:1000000;'><table width='100%' height='100%'><tr><td>"+wTitle+"</td><td style='width:1px;'><input type='button' onclick='closeWindow();' title='关闭窗口' style='height:15px;width:28px;border:none;cursor:pointer;text-decoration:underline;background:#fff' value='关闭' /></td></tr></table></div><div style='margin:4px;font-size:12px;' id='mesWindowContent'>"+content+"</div><div class='mesWindowBottom'></div>"; 
	styleStr+="left:"+(((pos.x-wWidth)>0)?(pos.x-wWidth):pos.x)+"px;top:"+(pos.y)+"px;position:absolute;width:"+wWidth+"px;"; 
	mesW.style.cssText=styleStr; 
	document.body.appendChild(mesW); 
}

//关闭窗口 
function closeWindow() 
{ 
	if(document.getElementById('back')!=null) 
	{ 
		document.getElementById('back').parentNode.removeChild(document.getElementById('back')); 
	} 
	if(document.getElementById('mesWindow')!=null) 
	{ 
		document.getElementById('mesWindow').parentNode.removeChild(document.getElementById('mesWindow')); 
	} 
	if(isIe){ 
	setSelectState('');} 
}

//设置select的可见状态 
function setSelectState(state) 
{ 
	var objl=document.getElementsByTagName('select'); 
	for(var i=0;i<objl.length;i++) 
	{ 
		objl[i].style.visibility=state; 
	} 
}

//让背景渐渐变暗 
function showBackground(obj,endInt) 
{ 
	if(isIe) 
	{ 
	obj.filters.alpha.opacity+=1; 
	if(obj.filters.alpha.opacity<endInt) 
	{ 
		setTimeout(function(){showBackground(obj,endInt)},5); 
	} 
	}else{ 
		var al=parseFloat(obj.style.opacity);al+=0.01; 
		obj.style.opacity=al; 
		if(al<(endInt/100)) 
		{setTimeout(function(){showBackground(obj,endInt)},5);} 
	} 
}
